package com.dao;

import com.model.Student;
import com.utils.DBUtils;

import java.sql.*;

public class StudentDao {


    public Student selectByAccount(String account){
        try {
            Connection connection = DBUtils.getConnection();
            String sql = "select * from t_student where stu_account = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,account);

            ResultSet resultSet = preparedStatement.executeQuery();
            // while ?
            if (resultSet.next()){

                int id = resultSet.getInt("id");
                String stuName = resultSet.getString("stu_name");
                String stuGender = resultSet.getString("stu_gender");
                Date stuBirther = resultSet.getDate("stu_birthday");
                String styAccount = resultSet.getString("stu_account");
                String stuPassword = resultSet.getString("stu_password");
                Student student = new Student(id, stuName, stuGender, stuBirther, styAccount, stuPassword);
                return student;
            }else{

                return null;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public int insert(Student student){
         return DBUtils.executeUpdate("insert into t_student(stu_name,stu_gender,stu_birthday,stu_account,stu_password) values(?,?,?,?,?)",
                student.getStuName(),
                student.getStuGender(),
                student.getStuBirthday(),
                student.getStuAccount(),
                student.getStuPassword()
                );
    }

}
